//
//  LJHMusicListViewController.m
//  HEARME
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHMusicListViewController.h"
#import "LJHAlbumsDetailCell.h"
#import "LJHPlayBtn.h"
#import "LJHPlayerViewController.h"
#import "LJHAlbumsDetailViewController.h"
#import "LJHAlbumListTableViewCell.h"
@interface LJHMusicListViewController ()<UITableViewDelegate,UITableViewDataSource,UMSocialUIDelegate>

@property(nonatomic,retain)UITableView *musicListTableView;
@property(nonatomic,retain)LJHBaseView *tableHeadView;
@property(nonatomic,retain)LJHBaseLable *titleLabel;
@property(nonatomic,retain)NSMutableArray *musicListArray;
@property(nonatomic,assign)CGFloat cellHeight;
@property(nonatomic,copy)NSString *cellCategory;

@end

@implementation LJHMusicListViewController
- (void)dealloc
{
    [_musicListTableView release];
    [_tableHeadView release];
    [_titleLabel release];
    
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    [[LJHPlayBtn shareButton].musicBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [LJHPlayBtn shareButton].frame =CGRectMake(WIDTH*0.4, HEIGHT-WIDTH*0.2-64, WIDTH*0.2, HEIGHT*0.2);
    [self.view addSubview :[LJHPlayBtn shareButton]];
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
        
        [[LJHPlayBtn shareButton] startCircle];
        
    }
    if ([LJHPlayerViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePaused) {
        
        [[LJHPlayBtn shareButton] stopCircle];
        
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
    [GiFHUD show];
    
    // Do any additional setup after loading the view.
    
    //初始化数组
    self.musicListArray = [NSMutableArray array];
    
    [self getData];
    [self createTableView];
    self.navigationItem.title = @"听单详情";
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.tabBarController.tabBar.hidden = YES;
    
    
    
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 创建tableView
-(void)createTableView
{
    self.musicListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    self.musicListTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.musicListTableView];
    [_musicListTableView release];
    self.musicListTableView.dataSource = self;
    self.musicListTableView.delegate = self;
    [self.musicListTableView registerClass:[LJHAlbumsDetailCell class] forCellReuseIdentifier:@"musicListTableCell"];
    [self.musicListTableView registerClass:[LJHAlbumListTableViewCell class] forCellReuseIdentifier:@"albumListTableCell"];
    self.musicListTableView.bounces = NO;
    self.musicListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self createHeadView];
    
    self.musicListTableView.tableHeaderView = self.tableHeadView;
    
    
    
}

-(void)buttonAction:(UIButton *)btn
{
    
    LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    
    [self.view.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
    }];
    
}

-(void)createHeadView
{
    self.tableHeadView = [[LJHBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH*0.2)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*0.05, WIDTH*0.05, WIDTH*0.1, WIDTH*0.1)];
    imageView.backgroundColor = [UIColor clearColor];
    imageView.image = [UIImage imageNamed:@"findsubject_cover@2x.png"];
    [self.tableHeadView addSubview:imageView];
    [imageView release];
    
    self.titleLabel = [[LJHBaseLable alloc] initWithFrame:CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+WIDTH*0.05, imageView.frame.origin.y-10, WIDTH*0.7, WIDTH*0.2)];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.text = @"";
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.font = [UIFont systemFontOfSize:17 weight:0];
    [self.tableHeadView addSubview:self.titleLabel];
    [_titleLabel release];

    
}

#pragma mark - 请求数据

-(void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=iphone&id=%@&position=1&title=精品听单", self.specialId];
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        //                NSLog(@"%@", result);
        NSMutableDictionary *infoDic = [result objectForKey:@"info"];
        self.titleLabel.text = [infoDic objectForKey:@"title"];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        NSString *str = [infoDic objectForKey:@"intro"];
        //        CGRect Fit = [str boundingRectWithSize:CGSizeMake(WIDTH-40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH*0.05, WIDTH*0.05, WIDTH*0.1, WIDTH*0.1)];
       
        self.cellCategory = [NSString stringWithFormat:@"%@", [infoDic objectForKey:@"contentType"]];
        
        //        [self.contentLabel sizeToFit];
        self.musicListTableView.tableHeaderView.frame = CGRectMake(0, 0, WIDTH, WIDTH/4);
        
        NSMutableArray *dataArray = [result objectForKey:@"list"];
        
        for (NSMutableDictionary *dic in dataArray) {
            
            if ([self.cellCategory isEqualToString:@"2"]) {
                LJHTrackModel *model = [LJHTrackModel modelWithDictionary:dic];
                [self.musicListArray addObject:model];
            }
            else{
                LJHAlbumModel *model = [LJHAlbumModel modelWithDictionary:dic];
                model.isFavor = NO;
                
                if ([[[LJHFMData shareFMDB] selectAlbumIdData] containsObject:model.myId]) {
                    model.isFavor = YES;
                }
                
                
                [self.musicListArray addObject:model];
            }
        }
        
        
        [self.musicListTableView reloadData];
    } error:^(NSError *error) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
}



#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    NSLog(@"%ld",self.musicListArray.count);
    return self.musicListArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.cellCategory isEqualToString:@"2"]) {
        LJHAlbumsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicListTableCell"];
        LJHTrackModel *model = [self.musicListArray objectAtIndex:indexPath.row];
        cell.trackModel = model;
        cell.playCount.text = [NSString stringWithFormat:@"%@",model.playsCounts];
        
        self.cellHeight = cell.playCount.frame.origin.y + cell.playCount.frame.size.height + 10;
        return cell;
    }
    else
    {
        LJHAlbumListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"albumListTableCell"];
        LJHAlbumModel *model = [self.musicListArray objectAtIndex:indexPath.row];
        cell.albumModel = model;
        self.cellHeight = cell.newsPic.frame.origin.y + cell.newsPic.frame.size.height + 10;
        return cell;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LJHBaseView *headView = [[LJHBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,10,10) ];
    arrowImageView.backgroundColor = [UIColor clearColor];
    arrowImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [headView addSubview:arrowImageView];
    [arrowImageView release];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(arrowImageView.frame.origin.x + arrowImageView.frame.size.width + 10, arrowImageView.frame.origin.y-5, 200*(WIDTH/375), 20)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = @"听单列表";
    [headView addSubview:titleLabel];
    [titleLabel release];
    
    
    return headView;
}
-(void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellHeight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT*0.04;
}

#pragma mark tableView点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.cellCategory isEqualToString:@"2"]) {
        LJHPlayerViewController *player = [LJHPlayerViewController sharaMusicPlayer];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
        LJHTrackModel *sing = [self.musicListArray objectAtIndex:indexPath.row];
        player.song_id = sing.trackId;
        player.allMusicArray = self.musicListArray;
        player.indexRow = indexPath.row;
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
        
    }
    else
    {
        LJHAlbumsDetailViewController *albumsDetailVC = [[LJHAlbumsDetailViewController alloc] init];
        albumsDetailVC.albumId = ((LJHAlbumModel *)[self.musicListArray objectAtIndex:indexPath.row]).myId;
        albumsDetailVC.myTitle = _myTitle;
        [(LJHTabBarViewController *)albumsDetailVC.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
